package security;

/**
 *
 * @author MONESI
 */
import boundary.SearchManager;
import boundary.UserManager;
import entity.ResearchUser;
import entity.User;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.persistence.Query;

@Named
@SessionScoped
public class SearchBean implements Serializable {
    
    @EJB
    private SearchManager sm;
    
    @EJB
    private UserManager um;
    
    ResearchUser user;
    
    User utente;

    public SearchBean() {
    }
    
     public ResearchUser getUser() {
        if (user == null) {
            user = new ResearchUser();
        }
        return user;
    }
    
    
    public void setUser (ResearchUser user) {
        this.user = user;
    }
    
    public String nome(){
        return sm.name();
    }
    
       
    
    public String research() {
        int esiste=0;
        
        for(int i=0;i<um.utenti().size() && esiste==0;i++){     
            if(user.getSearchedUser().equals(um.utenti().get(i).getEmail())){
                esiste=1;    
            }
        }
        if(esiste==1){
            
            user.setMail(um.getLoggedUser().getEmail());
            sm.save(user);
        
        
            if(sm.privacy().equals("Public")){ 
        
                return "ScheduleViewSearchedUser";
            
            } else {
            
            return "PrivatePage";
            
            }
        }else
            return "NotExist";
        
        
    }
    

}
